package cn.lsnu.modules.watch.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.modules.watch.entity.WatchAddEntity;
import cn.lsnu.modules.watch.service.WatchNearService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping({"/app/watchnear","/watchadd"})
public class WatchNearController extends BaseController {

    @Autowired
    private WatchNearService watchNearService;

    @Autowired
    private MenuService menuService;

    @RequestMapping({"/maplist","/addlist"})
    public
    @ResponseBody List<WatchAddEntity>
    mapList(WatchAddEntity entity) throws Exception{
        List<WatchAddEntity> mapLists =   watchNearService.findList(entity);
        return mapLists;
    }


    @RequestMapping({"/list"})
    public
    String List(MenuEntity entity, Model model, HttpSession session) throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("信息管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/modules/address/watchadd-list";
    }

    @RequestMapping({"/listjson"})
    public
    @ResponseBody
    Page<WatchAddEntity>
    listJson(WatchAddEntity entity) throws Exception{
        return  watchNearService.findPage(entity);
    }


    @RequestMapping({"/save"})
    public void save(WatchAddEntity entity) throws Exception{
        watchNearService.save(entity);
    }

    @RequestMapping("/updmsg")
    public String updMsg(String key,Model model)throws Exception{
        WatchAddEntity  entity = watchNearService.get(key);
        model.addAttribute("updwatchadd",entity);
        return "webpage/modules/address/watchadd-upd";
    }

    @RequestMapping("/upd")
    public void upd(WatchAddEntity entity)throws Exception{
        watchNearService.update(entity);
    }
    @RequestMapping(value = "/del",method= RequestMethod.POST)
    public
    @ResponseBody WatchAddEntity
    delete(WatchAddEntity entity)throws Exception{
        watchNearService.delete(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        return entity;
    }


}
